package com.zxl.yjb.activity.home.order;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zxl.yjb.GlideApp;
import com.zxl.yjb.R;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.http.HostUrl;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.utils.ToastUtils;
import com.zxl.yjb.widget.CircleImageView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.zhanghai.android.materialratingbar.MaterialRatingBar;

public class CommentActivity extends BaseActivity {

    @BindView(R.id.iv_head_img)
    CircleImageView ivHeadImg;
    @BindView(R.id.tv_order_title)
    TextView tvOrderTitle;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_order_id)
    TextView tvOrderId;
    @BindView(R.id.btn_comment)
    Button btnComment;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.rb_star)
    MaterialRatingBar rbStar;
    @BindView(R.id.tv_page_title)
    TextView tvPageTitle;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    private String friend_id;
    private String friend_img;
    private String title;
    private String time;
    private String order_sn;
    private int rating;
    private LoginSucessBean loginSucessBean;
    private String from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        ButterKnife.bind(this);
        Object bean = SaveBeanUtil.getBean(this, Const.VIPINFO);
        if (bean != null) {
            loginSucessBean = (LoginSucessBean) bean;
        }
        Intent intent = getIntent();
        friend_id = intent.getStringExtra("friend_id");
        friend_img = intent.getStringExtra("friend_img");
        title = intent.getStringExtra("title");
        time = intent.getStringExtra("time");
        order_sn = intent.getStringExtra("order_sn");
        from = intent.getStringExtra("from");
        initView();
        setUserInfo();
    }


    @Override
    protected void initView() {
        tvPageTitle.setText("发表评论");
        btnSubmit.setClickable(false);

        rbStar.setOnRatingChangeListener(new MaterialRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChanged(MaterialRatingBar ratingBar, float rating) {
                CommentActivity.this.rating = (int) rating;
                btnSubmit.setClickable(true);
                btnSubmit.setBackgroundResource(R.drawable.btn_submit);
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    /*btnSubmit.setClickable(true);
                    btnSubmit.setBackgroundResource(R.drawable.btn_submit);*/
                }
            }
        });

    }

    private void setUserInfo() {
        tvOrderTitle.setText(title);
        tvOrderId.setText(order_sn);
        tvOrderTime.setText(time);
        GlideApp.with(getApplication()).load(HostUrl.HOSTPIC + friend_img).into(ivHeadImg);

    }

    @OnClick({R.id.iv_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_submit://评价
                evaluate();
                break;
        }
    }

    /**
     * 评价
     * 参数：             //session 得到，uid 比如买家（评价商品的人）
     * evaluatebyid  ;//  被评价人，比如卖家（买家评价商品），
     * Star //星级1-5
     * Text  //内容
     */
    private void evaluate() {

        Map<String, String> map = new HashMap<>();
        map.put("session", loginSucessBean.getUid() + "");
        map.put("evaluatebyid", friend_id);
        map.put("star", rating + "");
        map.put("text", etContent.getText().toString().trim());
        map.put("order_sn", order_sn);
        map.put("request1response2", "send_list".equals(from)?"1":"2");
        DataManager dataManager = new DataManager();
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
                ToastUtils.show(getApplication(), "发布成功！");
                finish();
            }
        };
        dataManager.evaluate(new ProgressObserver<Object>(listener, this) {
            @Override
            public void showDialog() {
            }
        }, map);
    }
}
